package com.itcast.donkey.activity;


import android.annotation.TargetApi;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.itcast.donkey.R;
import com.itcast.donkey.adapters.TravelAdapter;
import com.itcast.donkey.fragments.AttackFragment;
import com.itcast.donkey.fragments.ToolBoxFragment;
import com.itcast.donkey.fragments.TravelFragment;
import com.itcast.donkey.popwindow.PopWindowsMain;

import java.util.ArrayList;

public class MainActivity extends FragmentActivity {
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private TravelAdapter adapter;
    private ImageView search, person, overflow;
    private RelativeLayout rel_title;
    //侧滑菜单
    private PopWindowsMain popWindows;
    private ArrayList<Fragment> lists = new ArrayList<Fragment>();
    String[] title = {"游记", "攻略", "工具箱"};
    TravelFragment travelFragment;
    AttackFragment attackFragment;
    ToolBoxFragment toolBoxFragment;
    private int i = 0;

    private SharedPreferences sp;
    private boolean isFirst = true;

    private boolean isEnter=true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sp = getSharedPreferences("config", MODE_PRIVATE);
        isFirst = sp.getBoolean("isFirst", true);
        isEnter=sp.getBoolean("isEnter",true);
        //讯飞语音
        //初始化语音配置对象，初始化后才可以使用MSC的各项服务
        //SpeechUtility.createUtility(MainActivity.this, SpeechConstant.APPID + "=57cea70c" + SpeechConstant.FORCE_LOGIN + "=true");
        initView();
        initEvent();
        initData();
    }

    private void initView() {
        popWindows = new PopWindowsMain(MainActivity.this, itemOnClickListener);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        search = (ImageView) findViewById(R.id.search);
        person = (ImageView) findViewById(R.id.person);
        overflow = (ImageView) findViewById(R.id.overflow);
        rel_title = (RelativeLayout) findViewById(R.id.rel_title);
    }

    private void initData() {
        Intent intent = getIntent();
        i = intent.getIntExtra("xia", 0);
        travelFragment = new TravelFragment();
        attackFragment = new AttackFragment();
        toolBoxFragment = new ToolBoxFragment();
        lists.add(travelFragment);
        lists.add(attackFragment);
        lists.add(toolBoxFragment);

        adapter = new TravelAdapter(getSupportFragmentManager(), lists, title);
        viewPager.setAdapter(adapter);

        tabLayout.setupWithViewPager(viewPager);

        viewPager.setCurrentItem(i);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 2 && isFirst == true) {
                    Intent intent1 = new Intent(MainActivity.this, ToolBoxAreaActivity.class);
                    startActivityForResult(intent1, 100);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    //popWindows
    private void initEvent() {


        overflow.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View v) {

                //popWindows.showAtLocation(MainActivity.this.findViewById(R.id.tabs), Gravity.TOP | Gravity.RIGHT, 0, 0);
                popWindows.showAsDropDown(rel_title, 0, 0, Gravity.RIGHT);
            }
        });
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SeachActivity.class);
                startActivity(intent);
            }
        });
        person.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isEnter){
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(MainActivity.this, PersonalActivity.class);
                    startActivity(intent);
                }

            }
        });
    }

    //pop中的三个按钮的点击事件
    private View.OnClickListener itemOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.pop_browse:
                    Intent intent = new Intent(MainActivity.this, OffLineBrowerActivity.class);
                    startActivity(intent);
                    break;
                case R.id.pop_feedback:
                    Intent intent1 = new Intent(MainActivity.this, FeedBackActivity.class);
                    startActivity(intent1);
                    break;
                case R.id.pop_set_up:
                    Intent intent2 = new Intent(MainActivity.this, SetUpActivity.class);
                    startActivity(intent2);
                    break;
            }
        }
    };


}

